﻿
namespace WinFormsApp_ImitateMouseAndKeyboard
{

    /// <summary>
    /// Mouse 的摘要说明。
    /// </summary>
    public class Mouse
    {
        internal const byte SM_MOUSEPRESENT = 19;
        internal const byte SM_CMOUSEBUTTONS = 43;
        internal const byte SM_MOUSEWHEELPRESENT = 75;

        public const int MOUSEEVENTF_LEFTDOWN = 0x2;
        public const int MOUSEEVENTF_LEFTUP = 0x4;
        public const int MOUSEEVENTF_MIDDLEDOWN = 0x20;
        public const int MOUSEEVENTF_MIDDLEUP = 0x40;
        public const int MOUSEEVENTF_MOVE = 0x1;
        public const int MOUSEEVENTF_RIGHTDOWN = 0x8;
        public const int MOUSEEVENTF_RIGHTUP = 0x10;

        public struct POINTAPI
        {
            public int x;
            public int y;
        }

        public struct RECT
        {
            public int left;
            public int top;
            public int right;
            public int bottom;
        }

        [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SwapMouseButton")]
        public extern static int SwapMouseButton(int bSwap);

        [System.Runtime.InteropServices.DllImport("user32", EntryPoint = "ClipCursor")]
        public extern static int ClipCursor(ref RECT lpRect);

        [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "GetCursorPos")]
        public extern static int GetCursorPos(ref POINTAPI lpPoint);

        [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "ShowCursor")]
        public extern static bool ShowCursor(bool bShow);

        [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "EnableWindow")]
        public extern static int EnableWindow(int hwnd, int fEnable);

        [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "GetWindowRect")]
        public extern static int GetWindowRect(int hwnd, ref RECT lpRect);

        [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SetCursorPos")]
        public extern static int SetCursorPos(int x, int y);

        [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "GetSystemMetrics")]
        public extern static int GetSystemMetrics(int nIndex);

        [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SetDoubleClickTime")]
        public extern static int SetDoubleClickTime(int wCount);

        [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "GetDoubleClickTime")]
        public extern static int GetDoubleClickTime();

        [System.Runtime.InteropServices.DllImport("kernel32.DLL", EntryPoint = "Sleep")]
        public extern static void Sleep(int dwMilliseconds);

        [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "mouse_event")]
        public static extern void mouse_event(
             int dwFlags,
             int dx,
             int dy,
             int cButtons,
             int dwExtraInfo
             );

        //得到鼠标相对与全屏的坐标，不是相对与你的Form的，且与你的分辨率有关系

        public static int FullScreenPosition_X
        {
            get
            {
                POINTAPI _POINTAPI = new POINTAPI();

                GetCursorPos(ref _POINTAPI);

                return _POINTAPI.x;
            }
        }

        public static int FullScreenPosition_Y
        {
            get
            {
                POINTAPI _POINTAPI = new POINTAPI();

                GetCursorPos(ref _POINTAPI);

                return _POINTAPI.y;
            }
        }

        // 隐藏 显示 鼠标

        public static void Hide()
        {
            ShowCursor(false);
        }

        public static void Show()
        {
            ShowCursor(true);
        }

        // 将鼠标锁定在你的Form里 不过你得将你的Form先锁了,Form Resize 就失效了

        public static void Lock(System.Windows.Forms.Form ObjectForm)
        {
            RECT _FormRect = new RECT();

            GetWindowRect(ObjectForm.Handle.ToInt32(), ref _FormRect);

            ClipCursor(ref _FormRect);
        }

        public static void UnLock()
        {
            RECT _ScreenRect = new RECT();

            _ScreenRect.top = 0;
            _ScreenRect.left = 0;
            _ScreenRect.bottom = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Bottom;
            _ScreenRect.right = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Right;

            ClipCursor(ref _ScreenRect);
        }

        // 鼠标失效,不过失效的好像不只是鼠标,小心哦

        public static void Disable(System.Windows.Forms.Form ObjectForm)
        {
            EnableWindow(ObjectForm.Handle.ToInt32(), 0);
        }

        public static void Enable(System.Windows.Forms.Form ObjectForm)
        {
            EnableWindow(ObjectForm.Handle.ToInt32(), 1);
        }

        // 鼠标自己移动 很想动画哦 参数是2个控件的handle
        // 看这个方法前，先用凉水擦把脸。。。 反正我写的时候 头晕

        public static void Move(int From_Handle_ToInt32, int To_Handle_ToInt32)
        {
            RECT rectFrom = new RECT();
            RECT rectTo = new RECT();

            int i;

            GetWindowRect(From_Handle_ToInt32, ref rectFrom);
            GetWindowRect(To_Handle_ToInt32, ref rectTo);

            if ((rectFrom.left + rectFrom.right) / 2 - (rectTo.left + rectTo.right) / 2 > 0)
            {
                for (i = (rectFrom.left + rectFrom.right) / 2; i >= (rectTo.left + rectTo.right) / 2; i--)
                {
                    SetCursorPos(i, (rectFrom.top + rectFrom.bottom) / 2);
                    Sleep(1);
                }
            }
            else
            {
                for (i = (rectFrom.left + rectFrom.right) / 2; i <= (rectTo.left + rectTo.right) / 2; i++)
                {
                    SetCursorPos(i, (rectFrom.top + rectFrom.bottom) / 2);
                    Sleep(1);
                }
            }

            if ((rectFrom.top + rectFrom.bottom) / 2 - (rectTo.top + rectTo.bottom) / 2 > 0)
            {
                for (i = (rectFrom.top + rectFrom.bottom) / 2; i >= (rectTo.top + rectTo.bottom) / 2; i--)
                {
                    SetCursorPos((rectTo.left + rectTo.right) / 2, i);
                    Sleep(1);
                }
            }
            else
            {
                for (i = (rectFrom.top + rectFrom.bottom) / 2; i <= (rectTo.top + rectTo.bottom) / 2; i++)
                {
                    SetCursorPos((rectTo.left + rectTo.right) / 2, i);
                    Sleep(1);
                }
            }
        }

        // 得到你的鼠标类型

        public static string Type
        {
            get
            {
                if (GetSystemMetrics(SM_MOUSEPRESENT) == 0)
                {
                    return "本计算机尚未安装鼠标";
                }
                else
                {
                    if (GetSystemMetrics(SM_MOUSEWHEELPRESENT) != 0)
                    {
                        return GetSystemMetrics(SM_CMOUSEBUTTONS) + "键滚轮鼠标";
                    }
                    else
                    {
                        return GetSystemMetrics(SM_CMOUSEBUTTONS) + "键鼠标";
                    }
                }
            }
        }

        // 设置鼠标双击时间

        public static void DoubleClickTime_Set(int MouseDoubleClickTime)
        {
            SetDoubleClickTime(MouseDoubleClickTime);
        }

        public static string DoubleClickTime_Get()
        {
            return GetDoubleClickTime().ToString();
        }

        // 设置鼠标默认主键 我是没有见过谁左手用鼠标

        public static void DefaultRightButton()
        {
            SwapMouseButton(1);
        }

        public static void DefaultLeftButton()
        {
            SwapMouseButton(0);
        }
        private static void LeftDown()
        {
            mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
        }
        private static void LeftUp()
        {
            mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
        }
        public static void LeftClick()
        {
            LeftDown();
            LeftUp();
        }
        private static void MiddleDown()
        {
            mouse_event(MOUSEEVENTF_MIDDLEDOWN, 0, 0, 0, 0);
        }
        private static void MiddleUp()
        {
            mouse_event(MOUSEEVENTF_MIDDLEUP, 0, 0, 0, 0);
        }
        public static void MiddleClick()
        {
            MiddleDown();
            MiddleUp();
        }
        private static void RightDown()
        {
            mouse_event(MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0);
        }
        private static void RightUp()
        {
            mouse_event(MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0);
        }
        public static void RightClick()
        {
            RightDown();
            RightUp();
        }

    }
}
